package com.love.marriage.publicInfoConfig.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.love.marriage.publicInfoConfig.entity.PublicInfoConfig;
import com.love.marriage.publicInfoConfig.mapper.PublicInfoConfigMapper;
import com.love.marriage.publicInfoConfig.service.PublicInfoConfigService;
import com.love.marriage.publicInfoConfig.vo.PublicInfoConfigVo;
import com.love.marriage.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * description:  公共信息配置表服务实现类
 * author:       mybatisPlus
 * createDate:   2024-10-27 10:33:49
 * version:      1.0.0
 */
@Service
@RequiredArgsConstructor
public class PublicInfoConfigServiceImp extends ServiceImpl<PublicInfoConfigMapper, PublicInfoConfig> implements PublicInfoConfigService {

    private final PublicInfoConfigMapper publicInfoConfigMapper;

    @Override
    public Page<PublicInfoConfigVo> getPage(Long pageNum, Long pageSize, PublicInfoConfigVo publicInfoConfigVo) {
        Page<PublicInfoConfigVo> page = new Page<>(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        return publicInfoConfigMapper.getPage(page, publicInfoConfigVo);
    }

    @Override
    public PublicInfoConfigVo getPublicInfo(String code) {
        PublicInfoConfigVo publicInfo = publicInfoConfigMapper.getPublicInfo(code);
        if (publicInfo != null && StringUtils.isNotBlank(publicInfo.getDescription())) {
            publicInfo.setDescription(publicInfo.getDescription().replace("\n", "").replaceAll("(?i)<br\\s*/?>", "\n"));
        }
        return publicInfo;
    }


    @Override
    public PublicInfoConfigVo queryPublicInfoConfig(Long id) {
        return publicInfoConfigMapper.queryPublicInfoConfig(id);
    }

    @Override
    public PublicInfoConfigVo addPublicInfoConfig(PublicInfoConfigVo publicInfoConfigVo) {
        PublicInfoConfigVo db = getPublicInfo(publicInfoConfigVo.getCode());
        if (db != null) {
            throw new RuntimeException("编码已存在");
        }
        PublicInfoConfig publicInfoConfig = new PublicInfoConfig();
        BeanUtils.copyProperties(publicInfoConfigVo, publicInfoConfig);
        publicInfoConfigMapper.insert(publicInfoConfig);
        PublicInfoConfigVo res = new PublicInfoConfigVo();
        BeanUtils.copyProperties(publicInfoConfig, res);
        return res;
    }

    @Override
    public PublicInfoConfigVo updatePublicInfoConfig(PublicInfoConfigVo publicInfoConfigVo) {
        PublicInfoConfig publicInfoConfig = new PublicInfoConfig();
        BeanUtils.copyProperties(publicInfoConfigVo, publicInfoConfig);
        publicInfoConfigMapper.updateById(publicInfoConfig);
        return publicInfoConfigVo;
    }

    @Override
    public Boolean deletePublicInfoConfig(String ids) {
        if (StringUtils.isEmpty(ids)) {
            return true;
        }
        List<String> idArr = Arrays.asList(ids.split(","));
        publicInfoConfigMapper.deleteBatchIds(idArr);
        return true;
    }
}
